package com.woniuxy.yoga.course.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.statedata.ProductState;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import com.woniuxy.yoga.course.dao.model.TeamCourse;
import com.woniuxy.yoga.course.model.dto.team.TeamCourseDTO;
import com.woniuxy.yoga.course.model.form.team.AddTeamCourseForm;
import com.woniuxy.yoga.course.model.form.team.AmountTeamCourseDurationForm;
import com.woniuxy.yoga.course.model.form.team.CoachViewTeamCourseForm;
import com.woniuxy.yoga.course.model.form.team.QueryAllTeamCourseForm;
import com.woniuxy.yoga.course.model.param.team.AddTeamCourseParam;
import com.woniuxy.yoga.course.model.param.team.AmountTeamCourseDurationParam;
import com.woniuxy.yoga.course.model.param.team.CoachViewTeamCourseParam;
import com.woniuxy.yoga.course.model.param.team.QueryAllTeamCourseParam;
import com.woniuxy.yoga.course.service.service.TeamCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 团课课程表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/teamCourse")
@Api(tags = "团课课程相关业务接口")
@Validated
public class TeamCourseController {

    @Autowired
    private TeamCourseService teamCourseService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 教练ID查询团队课程
     * @param ids
     * @return
     */
    @PostMapping("/getByCoachId")
    @ApiOperation("教练ID查询团队课程接口")
    public Result<List<TeamCourseDTO>> getByCoachId(@RequestBody List<Integer> ids){
        //获取数据
        List<TeamCourseDTO> teamCourseDTOS = teamCourseService.getByCoachId(ids);
        //返回数据
        return Result.getListResult(TeamCourseDTO.class).setMessage("根据教练ID查询团队课程信息成功").setData(teamCourseDTOS);
    }

    /**
     * 团课id查询团课
     * @param teamCourseId 团课id
     * @return TeamCourseDTO
     */
    @GetMapping("/getByTeamCourseId/{teamCourseId}")
    @ApiOperation("团课id查询团课")
    public Result<TeamCourseDTO> getByTeamCourseId(@PathVariable @Valid @NotNull Integer teamCourseId){
        TeamCourse teamCourse = teamCourseService.getById(teamCourseId);
        TeamCourseDTO teamCourseDTO = BeanUtil.toBean(teamCourse, TeamCourseDTO.class);
        return Result.getResult(TeamCourseDTO.class).setMessage("团课id查询团课").setData(teamCourseDTO);
    }

    /**
     * 新增团队课程
     * @param addTeamCourseForm
     * @return
     */
    @PostMapping("/addTeamCourse")
    @ApiOperation("新增团队课程接口")
    @YogaLog("新增团队课程")
    public Result<?> addTeamCourse(@Valid @RequestBody AddTeamCourseForm addTeamCourseForm){
        teamCourseService.addTeamCourse(BeanUtil.toBean(addTeamCourseForm, AddTeamCourseParam.class));
        return Result.getResult().setMessage("新增团队课程成功");
    }

    /**
     * 修改成进行中
     * @param teamCourseId
     * @return
     */
    @GetMapping("/modifyTeamCourseHave.co")
    @ApiOperation("修改成进行中接口")
    @ApiImplicitParam(name = "teamCourseId",value = "团队课程ID")
    public Result<?> modifyTeamCourseHave(@Valid @NotNull(message = "团队课程ID不能为空") @PathVariable("teamCourseId") Integer teamCourseId){
        teamCourseService.modifyTeamCourseHave(teamCourseId);
        return Result.getResult().setMessage("修改成进行中成功");
    }

    /**
     * 修改成已完成(需要将该团队课程对应的团队产品总天数减一,到零的时候将团队产品状态该为已完成)
     * @param teamCourseId
     * @return
     */
    @GetMapping("/modifyTeamCourseComplete.co")
    @ApiOperation("修改成已完成接口")
    @ApiImplicitParam(name = "teamCourseId",value = "团队课程ID")
    public Result<?> modifyTeamCourseComplete(@Valid @NotNull(message = "团队课程ID不能为空") @PathVariable("teamCourseId") Integer teamCourseId){
        teamCourseService.modifyTeamCourseComplete(teamCourseId);
        return Result.getResult().setMessage("修改成已完成成功");
    }

    /**
     * 商家查询自己的所有的课程信息
     * @param queryAllTeamCourseForm
     * @return
     */
    @PostMapping("/queryAllTeamCourse.bu")
    @ApiOperation("商家查询自己的所有的课程信息接口")
    public Result<PageInfo> queryAllTeamCourse(@Valid @RequestBody QueryAllTeamCourseForm queryAllTeamCourseForm){
        PageInfo pageInfo = teamCourseService.queryAllTeamCourse(BeanUtil.toBean(queryAllTeamCourseForm, QueryAllTeamCourseParam.class));
        return Result.getResult(PageInfo.class).setMessage("商家查询自己的所有的课程信息成功").setData(pageInfo);
    }

    /**
     * 统计该时间段的团队课程已完成的总时长
     * @param amountTeamCourseDurationForm
     * @return
     */
    @PostMapping("/amountTeamCourseDuration")
    @ApiOperation("统计团课课程总时长接口")
    public Result<Integer> amountTeamCourseDuration(@RequestBody AmountTeamCourseDurationForm amountTeamCourseDurationForm){
        Integer duration = teamCourseService.amountTeamCourseDuration(BeanUtil.toBean(amountTeamCourseDurationForm, AmountTeamCourseDurationParam.class));
        return Result.getResult(Integer.class).setMessage("统计该时间段的团队课程已完成的总时长成功").setData(duration);
    }

    /**
     * 教练查询自己的团队课程
     * @param coachViewTeamCourseForm
     * @param authenToken
     * @return
     */
    @PostMapping("/coachViewTeamCourse.co")
    @ApiOperation("教练查询团队课程接口")
    public Result<PageInfo> coachViewTeamCourse(@RequestBody CoachViewTeamCourseForm coachViewTeamCourseForm,@RequestHeader("authen_token") String authenToken){
        //获取长token
        String refreshToken = redisUtil.get(authenToken);
        //解析长token 获取coachId
        Integer coachId = JWTUtil.getCoachId(refreshToken);
        //对象转换
        CoachViewTeamCourseParam param = BeanUtil.toBean(coachViewTeamCourseForm, CoachViewTeamCourseParam.class);
        //教练ID赋值
        param.setCoachId(coachId);
        //返回数据
        PageInfo pageInfo = teamCourseService.coachViewTeamCourse(param);
        return Result.getResult(PageInfo.class).setMessage("教练查询自己的团队课程成功").setData(pageInfo);
    }
}

